package com.platform.article.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.article.dao.ArticleDao;
import com.platform.article.model.ArticleDO;
import com.platform.article.model.ArticleDOField;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author shuc
 */
@Service
public class ArticleService {

    @Resource
    private ArticleDao articleDao;

    public ArticleDO getById(String id) {
        return Optional.ofNullable(articleDao.lambdaQuery()
                        .eq(ArticleDO::getId, id)
                        .one())
                .orElse(new ArticleDO());
    }

    public Boolean addReadCountById(String id) {
        return articleDao.lambdaUpdate()
                .eq(ArticleDO::getId, id)
                .setSql(String.format("`%s`=`%s`+1", ArticleDOField.READ_COUNT, ArticleDOField.READ_COUNT))
                .update();
    }

    public Boolean save(ArticleDO articleDO) {
        return articleDao.save(articleDO);
    }

    public Boolean updateById(ArticleDO articleDO) {
        return articleDao.updateById(articleDO);
    }

    public Boolean removeById(String id) {
        return articleDao.removeById(id);
    }

    public Page<ArticleDO> page(Page<ArticleDO> page) {
        return articleDao.page(page);
    }

    public Page<ArticleDO> pageByAuthorId(String authorId, Page<ArticleDO> page) {
        return articleDao.page(page,
                new LambdaQueryWrapper<ArticleDO>()
                        .eq(ArticleDO::getAuthorId, authorId));
    }

    public Page<ArticleDO> pageByStatus(Integer status, Page<ArticleDO> page) {
        return articleDao.page(page,
                new LambdaQueryWrapper<ArticleDO>()
                        .eq(ArticleDO::getStatus, status));
    }
}
